import 'package:loxen/mode/like_bean.dart';
import 'package:loxen/mode/study/study_bean.dart';
import 'package:loxen/mode/study/study_info_bean.dart';
import 'package:loxen/viewmode/base_view_model.dart';
import 'package:flutter/foundation.dart';
import 'package:loxen/api/api.dart';
import 'package:rxdart/rxdart.dart';

class MineStudyListViewModel extends BaseViewModel {
  Map<String, List<StudyInfo>> _map = {};

  //加载数据
  void loadData(String type, int folder) {
    ApiService.mineStudyTypeList(type, folder)
        .doOnListen(() {
          mState = BaseState.LOADING;
        })
        .flatMap((value) =>
            compute(decodeStudyBean, value.data).asStream())
        .listen((event) {
          if (event.data.results.length > 0) {
            _map.putIfAbsent('0', () => event.data.results);
            mState = BaseState.CONTENT;
          } else {
            mState = BaseState.EMPTY;
          }
        }, onError: (error, stackTrace) {
          mState = BaseState.FAIL;
          print(error);
          print(stackTrace);
        }, onDone: () => notifyListeners());
  }

  // 加载下一页数据
  Future<List<StudyInfo>> nextData(String type, int folder, int page) async {
    if (_map.containsKey(page.toString())) {
      return _map[page.toString()];
    } else {
      var data = await ApiService.mineStudyTypeList(type, folder,
              page: (page += 1).toString())
          .flatMap((value) =>
              compute(decodeStudyBean, value.data).asStream())
          .first;
      _map.putIfAbsent(page.toString(), () => data.data.results);
      return data.data.results;
    }
  }

  //刷新列表内容
  void refresh() {
    _map.clear();
  }

  //内容修改
  void changeEntity() {
    notifyListeners();
  }

  //点赞
  void isLike(StudyInfo entity, String type) {
    if (entity != null) {
      if (entity.is_like == 0) {
        ApiService.likeStudy(entity.id.toString(), type)
            .flatMap((value) => compute(decodeLikeBean, value.data).asStream())
            .listen((event) {
          if (event.code == 2000) {
            entity.is_like = 1;
            entity.like_num = event.data.like_num;
            notifyListeners();
          }
        }, onError: (error) => print(error));
      } else {
        //取消点赞
        ApiService.unLikeStudy(entity.id.toString(), type)
            .flatMap((value) => compute(decodeLikeBean, value.data).asStream())
            .listen((event) {
          if (event.code == 2000) {
            entity.is_like = 0;
            entity.like_num = event.data.like_num;
            notifyListeners();
          }
        }, onError: (error) => print(error));
      }
    }
  }

  // 收集
  void unCollection(StudyInfo entity, String type) {
    type = 'study-' + type;
    ApiService.unCollection(type, entity.id.toString()).listen((event) {
      if (event.statusCode == 200) {
        entity.is_collect = 0;
        entity.collect_num -= 1;
        notifyListeners();
      }
    }, onError: (error, stack) {
      print(error);
      print(stack);
    });
  }

  static StudyBean decodeStudyBean(dynamic json) {
    return StudyBean.fromJson(json);
  }

  static LikeBean decodeLikeBean(dynamic json) {
    return LikeBean.fromJson(json);
  }
}
